package com.ml.service.impl;

import com.ml.dao.BaseDao;
import com.ml.model.HrmDepartMent;
import com.ml.service.HrmDepartMentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

@Transactional
@Service()
public class HrmDepartMentServiceImpl implements HrmDepartMentService {

    @Autowired
    private BaseDao<HrmDepartMent> basedao;

    public HrmDepartMent getByID(Integer id){
        return  basedao.get(HrmDepartMent.class,id);
    }

    /**获取所有*/
    public List<HrmDepartMent> getByAllDepart(){
        return basedao.find(" from HrmDepartMent");
    }
    /**获取所有没有上级的部门*/
    public List<HrmDepartMent> getByAllParentdepart(){
        return basedao.find(" from HrmDepartMent where supdepid = '' or supdepid is null ");
    }


    /**通过部门ID，获取一个列表，该列表，包含所有下级部门*/
    public List<HrmDepartMent> getByAllNextDepart(Integer id){
        //每次调用清空以前的值
        lastChildren = new ArrayList<>();
        List<HrmDepartMent> allNext = getAllChild( id);
        if(id!=null){
            allNext.add(getByID(id));
        }
        return allNext;
    }
    /**通过部门ID，获取一个列表，该列表，包含所有下级部门 Str 0,1,2,3*/
    public String getByAllNextDepartStr(Integer id){
        //每次调用清空以前的值
        lastNextStr = "";

        String lastStr = getAllChildStr( id);
        if(id!=null ){
            if(lastStr!=null && lastStr.length()>0){
                lastStr = id  + "" + lastStr;
            }else{
                lastStr = id+"";
            }
        }
        if(lastStr==null || lastStr.length()<=0){
            lastStr = -2 +"";//没有查到随便返回一个负数，因为泛微的ID生成是大于0的
        }
        return lastStr;
    }



    private String lastNextStr = "";//要一个字符型
    public String getAllChildStr(Integer parentId) {
        List<HrmDepartMent> vos = listNext(parentId);
        for (HrmDepartMent root : vos) {
            String rootid = root.getId() + "";
            if(!lastNextStr.contains( rootid )){
                lastNextStr = lastNextStr + "," + root.getId();//多了一个逗号，在前
            }
            getAllChildStr( root.getId() );
        }
        return lastNextStr;
    }


    private List<HrmDepartMent> lastChildren = new ArrayList<>();

    public List<HrmDepartMent> getAllChild(Integer parentId) {
        List<HrmDepartMent> vos = listNext(parentId);
        for (HrmDepartMent root : vos) {
            if(!lastChildren.contains(root)){
                lastChildren.add(root);
            }
            if(root.getSupdepid()!=null){
                getAllChild( root.getId());
            }
        }
        return lastChildren;
    }

    public List<HrmDepartMent> listNext(Integer parentId) {
        String hql = "from HrmDepartMent where 1=1 ";
        Object[] val = new Object[]{} ;
        if (parentId!=null) {
            hql += " and supdepid = ? ";
            val = new Object[]{parentId} ;
        } else {
            hql += "and supdepid = '' or supdepid is null ";
        }
        return basedao.find(hql,val);


    }

    public String getByAllNextDepartStr2(Integer id){
        HrmDepartMent ml =  getByID(id);
        String departCode = ml.getDepartmentcode();
        String res =  "";
        if(StringUtils.hasText(departCode)){//有code
            List<HrmDepartMent> list = getByAllNext( departCode);
            if(list!=null && list.size()>0){
                for(HrmDepartMent d :list ){
                    String nid = d.getId()+"";
                    res  =   nid + "," + res ;
                }
                res = res.substring(0,res.length()-1);
            }
        }else{//没有code
            res = getByAllNextDepartStr(id);//调用元方法 和departcode无关
        }
        return  res;

    }

    /**通过部门departCode，获取一包含所有下级部门*/
    public List<HrmDepartMent> getByAllNext(String departCode){
        //String sql = " select id from hrmdepartment where departmentcode like '"+departCode+"%'";
        String hql = " from HrmDepartMent where departmentcode like '"+departCode+"%'";
        return  basedao.find(hql);
    }

}
